<?php
namespace app\api\command;

use app\common\service\App;
use app\common\service\AppService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

class FirstChat extends Command
{
    protected function configure()
    {
        //心动女方未回复处理
        $this->setName('first_chat')->setDescription('first_chat');
    }

    protected function execute(Input $input, Output $output)
    {
        /* 永不超时 */
        ini_set('max_execution_time', 0);
        $this->doCron();
    }


    public function doCron(){

        $cache = cache('command'.__CLASS__);
        if ($cache){
            echo 'running<br />';return false;
        }else{
            cache('command'.__CLASS__,true,1800);
        }
        $time = time();
        $map['is_return'] = 0;
        $map['is_give'] = 1;
        $map['is_reply'] = 0;
        $map['update_time'] = ['<',$time - App::getExplain('return_time')*60];

        Db::name('first_chat')->where($map)->chunk(1000,function ($items){
            foreach ($items as $k => $v){

                if ($v['is_beckoning'] == 1 || $v['is_beckoning'] == 2){

                    if ($v['is_beckoning'] == 1){
                        $get = App::getExplain('coin_get');
                    }else{
                        $get = App::getExplain('under_coin_get');
                    }

                    if ($v['price'] <= 0){
                        $v['price'] = App::getExplain('coin_pay');
                    }

                    //心动红包赠送
                    Db::startTrans();

                    try{
                        money_detail(1,$v['user_id'], '红包退回',$v['price'],'红包退回',0,$v['to_user'],2);
                        money_detail(2,$v['to_user'], '红包退回',$get,'红包退回',0,$v['user_id'],1);
                        Db::name('users')->where('user_id',$v['user_id'])->setInc('coin',$v['price']);
                        Db::name('first_chat')->where('id',$v['id'])->delete();

                        Db::commit();
                    }catch (\Exception $e){
                        Db::rollback();
                        dd($e->getMessage());
                    }
                    $str = sprintf('您于赠送给[%s]的心动红包因对方长时间未回复,已退回至余额',
                        AppService::getUserInfo('nick_name',$v['to_user']));
                    $str2 = sprintf('[%s]赠送给您的心动红包因您长时间未回复,已退回至对方余额',
                        AppService::getUserInfo('nick_name',$v['user_id']));

                    App::sendUsrMsg('system',hashid($v['user_id']),$str);
                    App::sendUsrMsg('system',hashid($v['to_user']),$str2);
                    
                }else{

                    $map['user_id'] = $v['user_id'];
                    $map['to_user'] = $v['to_user'];
                    $map['record_id'] = $v['record_id'];
//                $map['create_time'] = ['between',[$v['update_time']-3600,$v['update_time']+3600]];
//                    $map['gift_id'] = 25;

                    $record = Db::name('gift_record')->where($map)->find();
                    if ($record){
                        Db::startTrans();
                        try{

                            money_detail(1,$record['user_id'], '送礼退回',$record['total'],'心动退回');
                            money_detail(2,$record['to_user'], '送礼退回',$record['after_total'],'心动退回');
                            Db::name('users')->where('user_id',$record['user_id'])->setInc('money',$record['total']);
                            Db::name('first_chat')->where('id',$v['id'])->delete();
                            if ($record['record_id'] != 0){
                                Db::name('gift_record')->where('record_id',$record['record_id'])->delete();
                            }
                            Db::commit();
                        }catch (Exception $e){
                            Db::rollback();
                            dd($e->getMessage());
                        }
                        $gift_name = getGiftName($record['gift_id']);
                        $str = sprintf('您于%s赠送给[%s]的'.$gift_name.'因对方长时间未回复,已退回至余额',
                            date('Y-m-d H:i',$record['create_time']),
                            AppService::getUserInfo('nick_name',$record['to_user']));
                        $str2 = sprintf('[%s]于%s赠送给您的'.$gift_name.'因您长时间未回复,已退回至对方余额',
                            AppService::getUserInfo('nick_name',$record['user_id']),
                            date('Y-m-d H:i',$record['create_time']));

                        App::sendUsrMsg('system',hashid($record['user_id']),$str);
                        App::sendUsrMsg('system',hashid($record['to_user']),$str2);
                    }else{
//                        Db::name('first_chat')->where('id',$v['id'])->delete();
                    }

                }




            }
        });
        cache('command'.__CLASS__,null);

    }
}